package org.zhouhe.modules.upload;

import java.io.File;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.zhouhe.base.BaseAction;

public class Upload4CKEditorAction extends BaseAction {
	private static final long serialVersionUID = 1653060788696496243L;
	private File[] upload;
	private String[] uploadFileName;
	private String CKEditorFuncNum;

	public void upload() {
		try {
			String[] filePathInfo = UploadUtils.getImagePathInfo("article_img", uploadFileName[0]);
			UploadUtils.writeFile(this.upload[0], filePathInfo[1]);
			String[] args = new String[] { CKEditorFuncNum, filePathInfo[2], "" };
			HttpServletResponse response = ServletActionContext.getResponse();
			PrintWriter out = response.getWriter();
			out.print("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction('");
			out.print(StringUtils.join(args, "','"));
			out.print("');</script>");
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void setCKEditorFuncNum(String cKEditorFuncNum) {
		CKEditorFuncNum = cKEditorFuncNum;
	}

	public String getCKEditorFuncNum() {
		return CKEditorFuncNum;
	}

	public void setUpload(File[] upload) {
		this.upload = upload;
	}

	public File[] getUpload() {
		return upload;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String[] getUploadFileName() {
		return uploadFileName;
	}

}
